package cn.schoolwow.workflow.test.module.analyzeWorkReport.audit.single.common;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceQuery;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.entity.WorkFlowInstanceHistory;
import cn.schoolwow.workflow.test.domain.QuickWorkFlowTestOption;

import java.time.format.DateTimeFormatter;
import java.util.List;

public class PrintWorkFlowInstanceHistoryFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        WorkFlowInstance workFlowInstance = flowContext.getInstanceData(WorkFlowInstance.class);
        QuickWorkFlowTestOption quickWorkFlowTestOption = flowContext.checkInstanceData(QuickWorkFlowTestOption.class);

        if(null==workFlowInstance){
            return;
        }

        if(!quickWorkFlowTestOption.printHistory){
            return;
        }

        WorkFlowInstanceQuery workFlowInstanceQuery = new WorkFlowInstanceQuery();
        workFlowInstanceQuery.instanceIdList.add(workFlowInstance.getId());
        List<WorkFlowInstanceHistory> workFlowInstanceHistoryList = quickWorkFlow.getWorkFlowInstanceHistoryList(workFlowInstanceQuery);
        for(WorkFlowInstanceHistory workFlowInstanceHistory:workFlowInstanceHistoryList){
            System.out.println(workFlowInstanceHistory.getCreatedAt().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
                    +" "+workFlowInstanceHistory.getMessage()
                    +" 处理人:"+workFlowInstanceHistory.getAssigner()
            );
        }
        System.out.println();
    }

    @Override
    public String name() {
        return "测试用例-打印工作流实例记录";
    }
}
